package com.xintouyun.medicine.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xintoucloud.jdbcutil.JdbcUtil;
import com.xintoucloud.jdbcutil.RowMapper;
import com.xintouyun.medicine.dao.OrderItemDao;
import com.xintouyun.medicine.entity.Order;
import com.xintouyun.medicine.entity.OrderItem;
import com.xintouyun.medicine.model.DataGrid;
import com.xintouyun.medicine.service.OrderItemService;

public class OrderItemServiceImpl implements OrderItemService {
	OrderItemDao orderItemDao = new OrderItemDao();
		@Override
	public DataGrid <OrderItem> List(int page, int rows) {
		try {
			DataGrid <OrderItem> data=new DataGrid<>();
			int count=orderItemDao.count("select count(*) from m_orderitem",null);
			List<OrderItem> orderitems=orderItemDao.list("select oid,subprice,count,gid from m_orderitem limit ?,?",(page-1)*rows,rows);
			data.setTotal(count);
			data.setRows(orderitems);
			return data;
		}finally {
			JdbcUtil.closeConnection();
		}
	
		}
		@Override
		public DataGrid<OrderItem> show(int page, int rows, int pid) {
			try {
				DataGrid <OrderItem> data=new DataGrid<>();
				int count=orderItemDao.count("select * from m_orderitem oi,m_goods g,m_provider p,m_shop s where oi.gid=g.gid and g.sid=s.sid and s.pid=p.pid and p.pid='"+pid+"'",null);
				List<OrderItem> orders=orderItemDao.listJoin("select * from m_orderitem oi,m_goods g,m_provider p,m_shop s where oi.gid=g.gid and g.sid=s.sid and s.pid=p.pid and p.pid=? limit ?,?", new RowMapper<OrderItem>() {
					@Override
					public OrderItem mapRow(ResultSet rs) {	
						try {
							OrderItem orderitem=new OrderItem();
							orderitem.setCount(rs.getInt("count"));
							orderitem.setGid(rs.getInt("gid"));
							orderitem.setGname(rs.getString("gname"));
							orderitem.setOid(rs.getString("oid"));
							orderitem.setOiid(rs.getInt("oiid"));
							orderitem.setPrice(rs.getBigDecimal("price"));
							orderitem.setSubprice(rs.getBigDecimal("subprice"));
							return orderitem;
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return null;
					}}, pid,(page-1)*rows,rows);
				data.setTotal(count);
				data.setRows(orders);
				return data;
			}finally {
				JdbcUtil.closeConnection();
			}
		}
}
